package com.shu.es;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import com.shu.utils.HttpUtils;

public class EsTest {

	
	public static void main(String[] args) throws Exception {
		
		String s = HttpUtils.sendGet("http://127.0.0.1:9201/_cat/indices?pretty");
		//RestHighLevelClient client =  getClient();
		System.out.println(s.contains("not_scale_weight2"));
		//getData(client);
		//client.close();
	}
	
	public static RestHighLevelClient getClient() {
		List<HttpHost> list = new ArrayList<HttpHost>();
		list.add(new HttpHost("192.168.1.16", 9200, "http"));
		list.add(new HttpHost("192.168.1.17", 9200, "http"));
		HttpHost[] arr = new HttpHost[list.size()];
		arr = list.toArray(arr);
		RestClientBuilder builder = RestClient.builder(arr);
		
		return new RestHighLevelClient(builder);
	}
	
	/**
	 * https://www.cnblogs.com/wenbronk/p/6432990.html
	 * @param client
	 * @throws Exception
	 */
	public static void getData(RestHighLevelClient client) throws Exception {
		BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
		//boolQuery.must(QueryBuilders.termsQuery("id", "32b9510981974197bede002e8b7be05a"));
		//boolQuery.must(QueryBuilders.termQuery("official_accounts_name", "东莞城市推客"));
		RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("receiver_time").gt("20180107").lt("20180107");
		//rangeQuery.
		
	/*	QueryBuilder queryBuilder = QueryBuilders.boolQuery()
		            .must(QueryBuilders.termQuery("user", "kimchy"))
		            .mustNot(QueryBuilders.termQuery("message", "nihao"))
		            .should(QueryBuilders.termQuery("gender", "male"));
		boolQuery.must(QueryBuilders.rangeQuery("age").gt(10).lt(20));*/
		
		//searchSourceBuilder.setQuery(QueryBuilders.rangeQuery("age").gte(10).lte(20))  
		 
		//QueryBuilder queryBuilder = QueryBuilders.idsQuery().ids("1");
		
		//QueryBuilder queryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("name", "kimchy")).boost(2.0f);//
		
		//QueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("user", "kimch"); 模糊查询
		
		 QueryBuilder queryBuilder3 = QueryBuilders.rangeQuery("user")
		            .from("kimchy")
		            .to("wenbronk")
		            .includeLower(true)     // 包含上界
		            .includeUpper(true);      // 包含下届
		
		QueryBuilder queryBuilder2 = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("name", "kimchy")).boost(2.0f);
		 
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
/*		String groupByKey = "groupby_heartbeat_time";
		TermsAggregationBuilder aggregation = AggregationBuilders.terms(groupByKey).field("heartbeat_time");
		MaxAggregationBuilder maxField = AggregationBuilders.max("ttt").field("heartbeat_time");
		aggregation.subAggregation(maxField);
		*/
		
		
/*		TermsAggregationBuilder aggregation2 = AggregationBuilders.terms("groupby_use_type").field("use_type");
		//aggregation.subAggregation(aggregation2);
		aggregation2.subAggregation(AggregationBuilders.avg("avg_scale_longitude").field("scale_longitude"));
		
		searchSourceBuilder.aggregation(aggregation);
		*/
		searchSourceBuilder.query(boolQuery);
		
		String[] indes = {"scale_hb"};
		SearchRequest searchRequest = new SearchRequest(indes,searchSourceBuilder);
		SearchResponse search = client.search(searchRequest );
		System.out.println(search);
		Aggregations aggregations = search.getAggregations();
		//Aggregation aggregationResult = aggregations.get(groupByKey);
		//System.out.println(aggregationResult.getName());
		
		SearchHits hits = search.getHits();
		for(SearchHit hit : hits.getHits()) {
			System.out.println(hit.getSourceAsString());
		}
		System.out.println(hits.getHits());
	}
}
